<?php
General::getTpl()->assign('extraTitle',"Navigationen");

if(!Admin::checkPermission('canEditNavigation')) {
	General::ouputMessage('Fehler','Sie haben nicht gen&uuml;gend Rechte um diese Seite zu betrachten');
}else{

	if(isset($_GET['do']) && $_GET['do'] == "new") {
		// New Navigation?
		//General::getTpl()->assign('displayNaviForm',true);
		
		require_once 'kernel/form.php';
		
		// Form-Data
		$formField = array();
		$formField[0]['fieldName'] = "Name";
		$formField[0]['fieldType'] = Form::$TYPE_TEXT;
		$formField[0]['fieldDefaultValue'] = "";
		$formField[0]['fieldCheck'] = true;
		$formField[0]['fieldCheckExtra'] = Form::$CHECK_TEXT_NOTEMPTY;
		
		$form = new Form();
		$form->initByData("Navigation erstellen",true,"Speichern!",$formField);
		
		// Done?
		if($form->completeCheck()) {
			// Check Existence
			$query = DB::getInstance()->query("
				SELECT 
					id 
				FROM 
					".Tables::$naviTbl." 
				WHERE 
					naviName = '".DB::parseVar($_POST['Name'])."'");
			if(DB::getInstance()->numRows($query) > 0) {
				$form->addError("<li>Es existiert schon eine Navigation mit dem Namen</li>","Name");
				
				$form->outputFormular('form','formular.html');
			}else{
				// Create Navigation
				Navigation::createNavigation($_POST['Name']);
				General::ouputMessage('Erfolgreich','Die Navigation wurde erfolgreich gespeichert.','form');
			}
			
		}else{
			$form->outputFormular('form','formular.html');
		}
	}
	
	if(isset($_GET['do']) && $_GET['do'] == "edit" && isset($_GET['navId']) && is_numeric($_GET['navId'])) {
		// Edit Navigation?
		require_once 'kernel/form.php';
		
		// Get NaviName
		$query = DB::getInstance()->query("
			SELECT
				naviName
			FROM
				".Tables::$naviTbl."
			WHERE
				id = '".DB::parseVar($_GET['navId'])."'");
		$navName = DB::getInstance()->fetchArray($query);
		
		// Form-Data
		$formField = array();
		$formField[0]['fieldName'] = "Name";
		$formField[0]['fieldType'] = Form::$TYPE_TEXT;
		$formField[0]['fieldDefaultValue'] = $navName['naviName'];
		$formField[0]['fieldCheck'] = true;
		$formField[0]['fieldCheckExtra'] = Form::$CHECK_TEXT_NOTEMPTY;
		
		$form = new Form();
		$form->initByData("Navigation bearbeiten",true,"Speichern!",$formField);
		
		// Done?
		if($form->completeCheck()) {
			// Check Existence of new name
			$query = DB::getInstance()->query("
				SELECT 
					id 
				FROM 
					".Tables::$naviTbl." 
				WHERE 
					naviName = '".DB::parseVar($_POST['Name'])."' &&
					id != '".DB::parseVar($_GET['navId'])."'");
			if(DB::getInstance()->numRows($query) > 0) {
				$form->addError("<li>Es existiert schon eine Navigation mit dem Namen</li>","Name");
				
				$form->outputFormular('form','formular.html');
			}else{
				// Create Navigation
				Navigation::editNavigation($_GET['navId'],$_POST['Name']);
				General::ouputMessage('Erfolgreich','Die Navigation wurde erfolgreich bearbeitet.','form');
			}
			
		}else{
			$form->outputFormular('form','formular.html');
		}
	}
	
	if(isset($_GET['do']) && $_GET['do'] == "del" && isset($_GET['navId']) && is_numeric($_GET['navId'])) {
		require_once './kernel/formCheck.php';
		$form = new FormCheck("Bist du sicher?<br />Die Kategorien/Links der Navigation werden unwiderruflich gel&ouml;scht.","L&ouml;schen");
		// Done?
		if($form->completeCheck()) {
			if($form->isChecked()) {
				// Delete navigation
				Navigation::deleteNavigation($_GET['navId']);
			}
		}else{
			$form->outputFormular('form','formular.html');
		}
	}

	// Get Navigations
	General::getTpl()->assign('navigations',Navigation::getNavigations());
	
	General::getTpl()->assign('body',
		General::getTpl()->fetch('../admin/navigationOverview.html'));
}
?>